import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'

Vue.use(VueRouter)

const routes = [
  // 首页
  {
    path: '/',
    component: Index
  },
  // 看相似
  {
    path: '/smiler',
    component: () => import('../views/Smiler.vue')
  },
  // 优选
  {
    path: '/optimization',
    component: () => import('@/views/Optimization.vue')
  },
  // 购物车
  {
    path: '/shopc',
    component: () => import('../views/shopc.vue')
  },
  // 登录
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  },
  // 注册
  {
    path: '/registe',
    component: () => import('../views/Registe.vue')
  },
  // 注册
  {
    path: '/registed',
    component: () => import('../views/Registed.vue')
    // 分类
  },
  {
    path: '/classify',
    component: () => import('../views/classify.vue')
  },
  // 订单提交页
  {
    path: '/order',
    component: () => import('../views/order.vue'),
    meta: {
      mustLogin: true
    }
  },
  // 地址操作页
  {
    path: '/address',
    component: () => import('../views/address.vue'),
    meta: {
      mustLogin: true
    }
  },
  {
    path: '/adddz',
    component: () => import('../views/adddz.vue'),
    meta: {
      mustLogin: true
    }
  }, // 地址修改
  {
    path: '/uptaddress',
    component: () => import('../views/upaddress.vue'),
    meta: {
      mustLogin: true
    }
  },
  {
    path: '/pay',
    component: () => import('../views/pay.vue'),
    meta: {
      mustLogin: true
    }
  },
  // sworder支付完成
  {
    path: '/sworder',
    component: () => import('../views/sworder.vue'),
    meta: {
      mustLogin: true
    }
  },
  {
    path: '/me',
    component: () => import('../views/Me.vue'),
    meta: {
      mustLogin: true
    }
  },
  // 优选 商品详情
  {
    path: '/goods_details',
    component: () => import('@/views/Goods_details.vue')
  },
  // 搜索
  {
    path: '/search',
    component: () => import('../views/Search.vue')
  },
  // 修改用户名
  {
    path: '/username',
    component: () => import('../views/Username.vue'),
    meta: {
      mustLogin: true
    }
  },
  // 修改密码
  {
    path: '/password',
    component: () => import('../views/Pssword.vue'),
    meta: {
      mustLogin: true
    }
  },
  // 搜索详情页
  {
    path: '/search_details',
    component: () => import('../views/Search_details.vue')
  },
  // 支付密码修改
  {
    path: '/paypassword',
    component: () => import('../views/PayPassword.vue'),
    meta: {
      mustLogin: true
    }
  },
  // 评论
  {
    path: '/comment',
    component: () => import('../views/Comment.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta !== undefined && to.meta.mustLogin !== undefined && to.meta.mustLogin) {
    let token = sessionStorage.getItem('token')
    if (token === null) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
